package io.github.maojian.bianrysearch;

import org.junit.Test;

/**
 * @Auther: jian.mao
 * @Date: 2022-06-28 16:47
 * @Description: https://leetcode.cn/problems/two-sum-ii-input-array-is-sorted/
 */
public class Q167 {
    public int[] twoSum(int[] numbers, int target) {
        int length = numbers.length;
        int[] rtnArr = new int[2];
        for (int i = 0; i < length; i++) {
            int otherNum = target - numbers[i];
            int left = i + 1, right = length - 1;
            while (left <= right) {
                int mid = (right - left) / 2 + left;
                if (numbers[mid] == otherNum) {
                    rtnArr[0] = i + 1;
                    rtnArr[1] = mid + 1;
                    return rtnArr;
                } else if (numbers[mid] > otherNum) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            }
        }
        return new int[]{-1, -1};
    }

    @Test
    public void test() {
        int[] nums = new int[]{2, 7, 11, 15};
        int target = 9;
        int[] rtnArr = twoSum(nums, target);
        System.out.print(rtnArr[0]);
        System.out.print(",");
        System.out.print(rtnArr[1]);
    }
}
